﻿using System;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Text.RegularExpressions;
using System.Windows.Forms;


namespace GUI
{    
    delegate void KeyDownDelegate(TextBox TxtBox, KeyEventArgs e);

    public class BlockDelete
    {        
        private Collection<KeyDownDelegate> TxtKeyDownEvent = new Collection<KeyDownDelegate>();
        private Collection<TextBox> CollectionTextBox = new Collection<TextBox>();

        public BlockDelete()
        {
            this.OnKeyDown += delegate(TextBox TextBoxControl, KeyEventArgs e)
            {
                if (e.KeyCode == Keys.Delete)
                    e.Handled = true;
            };
        }
        
        event KeyDownDelegate OnKeyDown
        {
            add
            {
                TxtKeyDownEvent.Add(value);
                foreach (TextBox TextBoxControl in CollectionTextBox)
                    TextBoxControl.TextChanged += BringKeyDown;
            }
            remove
            {
                TxtKeyDownEvent.Remove(value);
                foreach (TextBox TextBoxControl in CollectionTextBox)
                    TextBoxControl.TextChanged -= BringKeyDown;
            }
        }

        void BringKeyDown(object sender, EventArgs e)
        {
            TextBox txtLocalTextBox = (TextBox)sender;
            KeyEventArgs oKeyEventArgs = (KeyEventArgs)e;
            foreach (KeyDownDelegate d in TxtKeyDownEvent)
                d.Invoke(txtLocalTextBox, oKeyEventArgs);
        }

        public void Function(TextBox txtTextBox)
        {
            txtTextBox.KeyDown += BringKeyDown;
        }
    }
}
